﻿Imports FinanceiroBLL
Imports FinanceiroDAL

Public Class ParcelaEditadaFRM
    Public objitpBLL As New itensCtasReceberBLL
    Public codicr, parcelas, codctr, codstatus As Integer
    Public status As String
    Dim calc As Decimal = 0
    Public valparcela As Decimal = 0
    Public valortotal, totalgrid As Decimal 'recebe de calc em editaparcelamento
    Public objitens As itensCtasReceberBLL
#Region "Salvar"
    Public Sub Salvar()
        Try
            If calc > 0 Then
                'objitens = New itensCtasReceberBLL
                objitens.ctrcodigo = objitens.ctrcodigo
                objitens.pagcodigo = CBFormaPgto.SelectedValue
                objitens.icrvalor = Decimal.Parse(TXTValorParcela.Text.Replace("R$", ""))
                objitens.icrparcela = Val(TXTParcela.Text)
                objitens.icrdtvencimento = DTPDtVencimento.Value
                codctr = Val(LCodigo.Text)
                status = "O" 'operação parcela editada
                Me.Close()
                limpar()
                '       objitens = Nothing
            Else
                MsgBox("Valor de parcela inválido! Parcela negativa", MsgBoxStyle.Critical)
            End If

        Catch ex As Exception
            MessageBox.Show("Erro ao gravar")
        End Try
    End Sub
#End Region
#Region "Carrega Campos"
    Public Sub Carregacampos()
        recalculaparcelas()
        LCodigo.Text = objitens.icrcodigo
        TXTParcela.Text = objitens.icrparcela
        TXTValorParcela.Text = Math.Round(calc, 2, MidpointRounding.AwayFromZero)
        TXTValorParcela_Validating("", Nothing)
        CBFormaPgto.SelectedValue = objitens.pagcodigo
        DTPDtVencimento.Text = objitens.icrdtvencimento
    End Sub
#End Region
#Region "LOAD"
    Private Sub ParcelaEditadaFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBFormaPgto, "Generico.Gen_FormaPagamento", "pag_codigo", "pag_descricao", "sta_codigo =1", "")
        If status = "E" Then
            'irá alterar...
            If parcelas = 1 Then
                TXTValorParcela.ReadOnly = True
            End If
            TXTValorParcela.Text = valparcela
            Carregacampos()
        Else
            limpar()
        End If
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        LCodigo.Text = ""
        TXTParcela.Text = ""
        TXTValorParcela.Text = "0,00"
        CBFormaPgto.SelectedValue = -1
    End Sub
#End Region
#Region "Recalcula Parcelas"
    Sub recalculaparcelas()

        TXTValorParcela.Text = (valparcela.ToString.Replace("R$", ""))
        valparcela = TXTValorParcela.Text
        totalgrid = Math.Round(totalgrid, 2, MidpointRounding.AwayFromZero)
        valparcela = Math.Round(valparcela, 2, MidpointRounding.AwayFromZero)
        valortotal = Math.Round(valortotal, 2, MidpointRounding.AwayFromZero)
        Try
            If codstatus = 4 Or codstatus = 0 Then
                If totalgrid = valortotal Then
                    calc = valortotal / parcelas
                    calc = Math.Round(calc, 2, MidpointRounding.AwayFromZero)
                End If
                If totalgrid > 0 Then
                    'parcelas = Val(LBNumRegistros.Text)
                    If totalgrid > valortotal Then
                        calc = (valparcela - (totalgrid - valortotal))
                        calc = Math.Round(calc, 2, MidpointRounding.AwayFromZero)
                    End If
                    If totalgrid < valortotal Then
                        calc = valparcela + (valortotal - totalgrid)
                        calc = Math.Round(calc, 2, MidpointRounding.AwayFromZero)
                    End If
                    If calc > 0 Then
                        TXTValorParcela.Text = (calc).ToString.Replace("R$", "")
                        TXTValorParcela.Text = FormatCurrency(TXTValorParcela.Text)
                    Else
                        MsgBox("Recalcule as parcelas anteriores. Valor maior que a parcela total.", MsgBoxStyle.Exclamation)
                        TXTParcela.SelectAll()
                    End If
                End If
            Else
                MsgBox("Parcela já fechada", MsgBoxStyle.Critical)
                TXTValorParcela.Text = valparcela
                TXTValorParcela.Text = FormatCurrency(TXTValorParcela.Text)
            End If

        Catch ex As Exception

        End Try
    End Sub
#End Region
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        limpar()
    End Sub

    Private Sub TSBSAlvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSAlvar.Click
        If TXTParcela.Text <> "" And TXTParcela.Text <> "0" Then
            If CBFormaPgto.SelectedValue <> 0 Then
                If TXTValorParcela.Text <> "" And TXTValorParcela.Text <> "0,00" Then
                    Salvar() 'salva
                Else
                    MsgBox("Verifique campo valor de parcela!", MsgBoxStyle.Information)
                    TXTValorParcela.SelectAll()
                End If
            Else
                MsgBox("Selecione uma forma de pagamento")
            End If
        Else
            MsgBox("Valor de parcela inválida", MsgBoxStyle.Information)
            TXTParcela.SelectAll()
        End If
    End Sub

    Sub valida()
        Dim valparcela As Decimal

        If (Decimal.TryParse(TXTValorParcela.Text.Replace("R$", ""), valparcela)) Then
        Else : valparcela = "0,00"
        End If
        If TXTValorParcela.Text <> "" Then
            TXTValorParcela.Text = FormatCurrency(TXTValorParcela.Text)
        Else
            TXTValorParcela.Text = "0,00"
        End If

        If TXTParcela.Text <> "" And TXTParcela.Text <> "0" Then
        Else
            MsgBox("Campo inválido!")
            TXTParcela.Select()
        End If
        If DTPDtVencimento.Value < (Date.Now).ToShortDateString Then
            MsgBox("Data de vencimento menor que a data atual", MsgBoxStyle.Exclamation)
            DTPDtVencimento.Select()
        End If
    End Sub

    Private Sub TXTParcela_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TXTParcela.Validating
        valida()
    End Sub

    Private Sub TXTValorParcela_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TXTValorParcela.Validating
        valida()
    End Sub

    Private Sub DTPDtVencimento_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles DTPDtVencimento.Validating
        valida()
    End Sub

    Private Sub DTPDtVencimento_Leave(sender As System.Object, e As System.EventArgs) Handles DTPDtVencimento.Leave
        valida()
    End Sub

    Private Sub TXTValorParcela_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorParcela.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTValorParcela.Text = FormatCurrency(TXTValorParcela.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub

    Private Sub TXTParcela_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTParcela.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub ParcelaEditadaFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
End Class